/**
 * Created by admin on 2017/3/23.
 */
var fs = require("fs");

var dofile = {
    getAllAlbums: function(callback){
    fs.readdir("./uploads",function(err,files){
        if(err){
            callback("没有找到uploads文件",null);
        }
        var allAlbums = [];
        (function iterator(i){
            if(i == files.length){
                //遍历结束
                console.log(allAlbums);
                callback(null,allAlbums);
                return;
            }
            fs.stat("./uploads/" + files[i],function(err,stats){
                if(err){
                    callback("找不到文件" + files[i] , null);
                }
                if(stats.isDirectory()){
                    allAlbums.push(files[i]);
                }
                iterator(i + 1);
            });
        })(0);
    })
    },
    getAllImagesByAlbumName : function(albumname,callback){
        fs.readdir("./uploads/"+albumname,function (err,files) {
            if(err){
                callback("没有找到uploads文件",null);
                return;
            }
            var allAlbums = [];
            (function iterator(i){
                if(i == files.length){
                    //遍历结束
                    console.log(allAlbums);
                    callback(null,allAlbums);
                    return;
                }
                fs.stat("./uploads/" + albumname + "/" + files[i],function(err,stats){
                    if(err){
                        callback("找不到文件" + files[i] , null);
                        return;
                    }
                    if(stats.isFile()){
                        allAlbums.push(files[i]);
                    }
                    iterator(i + 1);
                });
            })(0);
        });
    }
};
// 单元测试
//     dofile.getAllAlbums(function (parms) {
//         console.log(parms);
//     });
//暴露接口
module.exports = dofile;


